%Figure 7


clear all; close all;
para; 

iComp=50;

%Iterate over Lambda
%Choose lower iComp to speed up computation

LambdaSpace=linspace(0.975,1.08,iComp);



for comp=1:iComp
    
    
     Lambda=LambdaSpace(comp);

   
   
    
    
    %Calculate Second best outcome
    OptSB;
    vTauSB(comp)=tauSB;
    vQSB(comp)=qSB;
    vASB(comp)=aB;
    vFSB(comp)=FSB;
    vBetaSB(comp)=betaSB;
    
    %Calculate outcomes under moral hazard
    OptContract;
    Solve;
    vTau0(comp)=tau0;
    vQ(comp)=q;
    vAOut(comp)=a0;
    vFOut(comp)=F0-0.5*kappa*q^2;
    vBetaOut(comp)=beta0;
    
  
    
    
    
    
    
end 


space=LambdaSpace;
fFigure=figure(1); hold on;


subplot(3,4,3); hold on
 plot(space,smooth(vASB./vAOut-1)*100, 'k', 'Linewidth',3);
    xlabel('$\Lambda$','Interpreter','latex')
 ylabel('$\Delta a_0 (\%)$','Interpreter','latex')
 axis tight;
 
 subplot(3,4,7); hold on
 plot(space,smooth(vQSB./vQ-1,0.7,'rloess')*100, 'k', 'Linewidth',3);
    xlabel('$\Lambda$','Interpreter','latex')
 ylabel('$\Delta q^* (\%)$','Interpreter','latex')
 axis tight;

  subplot(3,4,11); hold on
 plot(space,smooth(vBetaSB./vBetaOut-1)*100, 'k', 'Linewidth',3);
    xlabel('$\Lambda$','Interpreter','latex')
 ylabel('$\Delta \beta_0 (\%)$','Interpreter','latex')
 axis tight;
 
 
 resize_plot(fFigure,2.8,10,13);

 print('CreditLambda3','-depsc')
 



 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%% Phi %%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 clear all; 
para; 

iComp=50;
%Choose lower iComp to speed up computation
%Iterate over phi
phiSpace=linspace(7,40,iComp);



for comp=1:iComp
    
     phi=phiSpace(comp);

   
   
    
    

    %Calculate second best outcomes
    OptSB;
    vTauSB(comp)=tauSB;
    vQSB(comp)=qSB;
    vASB(comp)=aB;
    vFSB(comp)=FSB;
    vBetaSB(comp)=betaSB;
    
    %Calculate outcomes under moral hazard
    OptContract;
    Solve;
    vTau0(comp)=tau0;
    vQ(comp)=q;
    vAOut(comp)=a0;
    vFOut(comp)=F0-0.5*kappa*q^2;
    vBetaOut(comp)=beta0;
    
  
    
    
    
    
    
end 

space=phiSpace;
fFigure=figure(1); hold on;


subplot(3,4,2); hold on
 plot(space,smooth(vASB./vAOut-1)*100, 'k', 'Linewidth',3);
 % plot(space,smooth(vTauDel), 'r:', 'Linewidth',3);
    xlabel('$\phi$','Interpreter','latex')
 ylabel('$\Delta a_0 (\%)$','Interpreter','latex')
 axis tight;
 
 subplot(3,4,6); hold on
 plot(space,smooth(vQSB./vQ-1)*100, 'k', 'Linewidth',3);
    xlabel('$\phi$','Interpreter','latex')
 ylabel('$\Delta q^* (\%)$','Interpreter','latex')
 axis tight;

  subplot(3,4,10); hold on
 plot(space,smooth(vBetaSB./vBetaOut-1)*100, 'k', 'Linewidth',3);
    xlabel('$\phi$','Interpreter','latex')
 ylabel('$\Delta \beta_0 (\%)$','Interpreter','latex')
 axis tight;
 
 

 
 

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%% kappa %%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


 clear all; 
para; 
qGrid=700;

iComp=50;
%Iterate over kappa
%Choose lower iComp to speed up computation

kappaSpace=linspace(7,40,iComp);



for comp=1:iComp
    
    kappa=kappaSpace(comp);
   

   
   
    
    %Calculate second best outcome
    OptSB;
    vTauSB(comp)=tauSB;
    vQSB(comp)=qSB;
    vASB(comp)=aB;
    vFSB(comp)=FSB;
    vBetaSB(comp)=betaSB;
    
    %Calculate Outcomes under Moral Hazard
    OptContract;
    Solve;
    vTau0(comp)=tau0;
    vQ(comp)=q;
    vAOut(comp)=a0;
    vFOut(comp)=F0-0.5*kappa*q^2;
    vBetaOut(comp)=beta0;
    
  
    
    
    
    
    
end 

space=kappaSpace;
fFigure=figure(1); hold on;


subplot(3,4,1); hold on
 plot(space,smooth(vASB./vAOut-1,0.7,'rloess')*100, 'k', 'Linewidth',3);
% plot(space,smooth(vASB./vAOut-1)*100, 'k', 'Linewidth',3);
 xlabel('$\kappa$','Interpreter','latex')
 ylabel('$\Delta a_0 (\%)$','Interpreter','latex')
 axis tight;
 
 subplot(3,4,5); hold on
 plot(space,smooth(vQSB./vQ-1,0.7,'rloess')*100, 'k', 'Linewidth',3);
 % plot(space,smooth(vQSB./vQ-1)*100, 'k', 'Linewidth',3);
    xlabel('$\kappa$','Interpreter','latex')
 ylabel('$\Delta q^* (\%)$','Interpreter','latex')
 axis tight;

  subplot(3,4,9); hold on
 plot(space,smooth(vBetaSB./vBetaOut-1,0.7,'rloess')*100, 'k', 'Linewidth',3);
 % plot(space,smooth(vBetaSB./vBetaOut-1)*100, 'k', 'Linewidth',3);
    xlabel('$\kappa$','Interpreter','latex')
 ylabel('$\Delta \beta_0 (\%)$','Interpreter','latex')
 axis tight;
 




 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%% gamma %%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
para; 

iComp=40;
%Iterate over gamma
%Choose lower iComp to speed up computation

gammaSpace=linspace(0.05,0.2,iComp);



for comp=1:iComp
    
  
   gamma=gammaSpace(comp);
   gammaBase=gamma;
   
   
    
    
    Agency=0;
    OptSB;
    vTauFB(comp)=tauSB;
    vQFB(comp)=qSB;
    vAFB(comp)=aB;
    Agency=1;
    
    OptSB;
    vTauSB(comp)=tauSB;
    vQSB(comp)=qSB;
    vASB(comp)=aB;
    vFSB(comp)=FSB;
    vBetaSB(comp)=betaSB;
    
    OptContract;
    Solve;
    vTau0(comp)=tau0;
    vQ(comp)=q;
    vAOut(comp)=a0;
    vFOut(comp)=F0-0.5*kappa*q^2;
    vBetaOut(comp)=beta0;
    
  
    
    
    
    
    
end 

space=gammaSpace;
fFigure=figure(1); hold on;


subplot(3,4,4); hold on
 plot(space,smooth(vASB./vAOut-1)*100, 'k', 'Linewidth',3);
 xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\Delta a_0 (\%)$','Interpreter','latex')
 axis tight;
 
 subplot(3,4,8); hold on
 plot(space,smooth(vQSB./vQ-1)*100, 'k', 'Linewidth',3);
    xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\Delta q^* (\%)$','Interpreter','latex')
 axis tight;

  subplot(3,4,12); hold on
 plot(space,smooth(vBetaSB./vBetaOut-1)*100, 'k', 'Linewidth',3);
    xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\Delta \beta_0 (\%)$','Interpreter','latex')
 axis tight;
 

 resize_plot(fFigure,13,7,13);

 print('CreditCollective','-depsc')

